Exercise 1_2_1: Data Types
Cute Cat
Introduction to R for Data Analysis
1
Create a numeric vector with at 6 numbers.
Clues
To create a vector you would need the
c()
function.
2
Now, create a character vector with four distinct strings.
Clues
Strings or characters have to be enclosed by quotation marks (
""
or
''
).
3
Let’s turn to more complex data structures. Create a matrix with two columns using the
matrix()
function based on the numeric vector you built in the first exercise.
Clues
You have to either define the
nrow
or
ncol
option to get the proper layout of the matrix.
4
Create a list from all the above defined data types.
Clues
Just wrap all elements in the
list()
function
5
Extract the matrix from the list and convert it to a data frame using the
as.data.frame()
function .
Clues
List elements are accessed with
[[]]
.
6
Add some column names (e.g., “first” and “second”) for the two columns of the data frame.
Clues
You would need the
names()
function.